import { defineConfig } from 'vitepress'
import fs from 'fs'
import path from 'path'

// 自动生成侧边栏配置函数
function generateSidebar(docsDir) {
  // 递归读取目录结构
  function readDirectory(dir, basePath = '') {
    const entries = fs.readdirSync(dir, { withFileTypes: true })
    const items = []

    entries.forEach(entry => {
      const fullPath = path.join(dir, entry.name)
      const relativePath = path.join(basePath, entry.name)
      
      if (entry.isDirectory()) {
        // 对于目录，递归读取其内容
        const children = readDirectory(fullPath, relativePath)
        
        // 只有当目录有内容时才添加到侧边栏
        if (children.length > 0) {
          items.push({
            text: formatTitle(entry.name),
            collapsed: true,
            items: children
          })
        }
      } else if (entry.isFile() && path.extname(entry.name) === '.md' && entry.name !== 'index.md') {
        // 对于 Markdown 文件，添加到侧边栏（跳过 index.md，它通常是主入口）
        const link = `/docs/${relativePath.replace(/\\/g, '/').replace('.md', '')}`
        items.push({
          text: formatTitle(path.basename(entry.name, '.md')),
          link: link
        })
      }
    })

    return items
  }

  // 格式化标题（将文件名转换为显示标题）
  function formatTitle(filename) {
    // 处理连字符和下划线
    const title = filename.replace(/[-_]/g, ' ')
    // 首字母大写
    return title.charAt(0).toUpperCase() + title.slice(1)
  }

  // 读取 docs 目录下的内容
  const items = readDirectory(docsDir)
  
  // 确保 index.md 始终在顶部
  const indexItem = {
    text: '总览',
    link: '/docs/index'
  }
  
  return [{
    text: '目录',
    items: [indexItem, ...items]
  }]
}

// 指定 docs 目录路径
const docsDir = path.resolve(__dirname, '../docs')
// 生成侧边栏配置
const sidebar = generateSidebar(docsDir)

// https://vitepress.dev/reference/site-config
export default defineConfig({
  title: "一直哭的椰子壳",
  description: "A VitePress Site",
  head: [['link', { rel: 'icon', href: '/icons/logo.png' }]],
  themeConfig: {
    // https://vitepress.dev/reference/default-theme-config
    nav: [
      { text: '主页', link: '/' },
      { text: '笔记', link: '/docs/index' },
      
      // { text: '笔记', 
      //   items:[
      //     {text: '总览',link: '/docs/index'},
      //     {text: '机器学习',link: '/docs/machineLearning/3dgs'},
      //     {text: '实用工具', link: '/docs/useful-tools/frps'},
      //     {text: 'Web前端', link: '/docs/web/front-end/vue'},
      //     {text: 'Web后端', link: '/docs/web/back-end/springBoot'}
      //   ]
      // }
    ],
    logo: '/icons/home.png',

    outline: {
      label: '大纲',
      level: [1, 4]
    },

    socialLinks: [
      { icon: 'github', link: 'https://github.com/Dryzk' },
      {
        icon: {
          svg: '<svg t="1753721571805" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5030" width="200" height="200"><path d="M512 1024C230.4 1024 0 793.6 0 512S230.4 0 512 0s512 230.4 512 512-230.4 512-512 512z m259.2-569.6H480c-12.8 0-25.6 12.8-25.6 25.6v64c0 12.8 12.8 25.6 25.6 25.6h176c12.8 0 25.6 12.8 25.6 25.6v12.8c0 41.6-35.2 76.8-76.8 76.8h-240c-12.8 0-25.6-12.8-25.6-25.6V416c0-41.6 35.2-76.8 76.8-76.8h355.2c12.8 0 25.6-12.8 25.6-25.6v-64c0-12.8-12.8-25.6-25.6-25.6H416c-105.6 0-188.8 86.4-188.8 188.8V768c0 12.8 12.8 25.6 25.6 25.6h374.4c92.8 0 169.6-76.8 169.6-169.6v-144c0-12.8-12.8-25.6-25.6-25.6z" fill="#888888" p-id="5031"></path></svg>'
         },
        link: 'https://gitee.com/yzk1217'
      },
      // {
      //   icon: {
      //     svg: '<svg t="1753760456084" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1487" width="200" height="200"><path d="M949.23174603 2.7936508H74.76825397C35.75873016 2.7936508 4.87619048 35.3015873 4.87619048 74.31111111v874.46349207c0 39.00952381 32.50793651 71.51746032 71.51746031 71.51746032H950.85714286c39.00952381 0 71.51746032-32.50793651 71.51746031-71.51746032V74.31111111C1020.74920635 35.3015873 988.24126984 2.7936508 949.23174603 2.7936508zM539.63174603 548.92698413h-149.53650793L365.71428571 651.32698413l35.75873016-30.88253968s81.26984127 92.64761905 95.8984127 115.4031746c14.62857143 22.75555556 1.62539683 105.65079365 1.62539683 105.65079365L360.83809524 669.20634921s-43.88571429 152.78730159-102.4 188.54603175c-58.51428571 34.13333333-118.65396825 9.75238095-118.65396826 9.75238095s82.8952381-60.13968254 128.40634921-136.53333334c43.88571429-76.39365079 58.51428571-180.41904762 58.51428571-180.41904761H154.41269841s13.0031746-61.76507937 35.75873016-63.39047619c22.75555556-1.62539683 136.53333333 0 136.53333333 0l-1.62539682-188.54603175-65.01587302 3.25079365s-13.0031746 47.13650794-48.76190476 73.14285714-56.88888889 16.25396825-56.88888889 16.25396826 63.39047619-177.16825397 82.8952381-212.92698413c19.5047619-35.75873016 76.39365079-39.00952381 76.39365079-39.00952381l-37.38412698 99.14920635h221.05396825c26.00634921 0 27.63174603 61.76507937 27.63174603 61.76507936h-136.53333333v185.2952381s91.02222222-3.25079365 120.27936508 0c30.88253968 3.25079365 30.88253968 63.39047619 30.88253968 63.39047619z m344.58412699 242.18412698h-138.15873016l-97.52380953 69.8920635-19.5047619-69.8920635h-55.26349206l1.62539682-559.13650793h308.82539683v559.13650793z" p-id="1488" fill="#8a8a8a"></path><path d="M676.16507937 798.06984127l81.26984126-63.39047619h65.01587302V302.32380952H640.40634921v432.35555556h16.25396825z" p-id="1489" fill="#8a8a8a"></path></svg>'
      //   },
      //   link: 'https://www.zhihu.com/people/ben-bao-bao-21-36/posts'
      // },
      {
        icon: {
          svg: '<svg t="1753760556163" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3390" width="200" height="200"><path d="M512 992C246.896 992 32 777.104 32 512 32 246.896 246.896 32 512 32c265.104 0 480 214.896 480 480 0 265.104-214.896 480-480 480z m16.00000031-387.67999969c32.67199969 4.08 64.48000031 4.8 96.31999969 1.968 21.6-1.92 42.01600031-7.84000031 60.16000031-20.544 35.82400031-25.12000031 46.67200031-79.872 22.73599969-115.36000031-13.71199969-20.352-34.512-28.44799969-56.95999969-33.28000031-32.86399969-7.07200031-66.048-5.39200031-99.23200031-3.19999969-5.31199969 0.336-6.336 2.88-6.78400031 7.69600031-2.88 30.64000031-6.12799969 61.23199969-9.19999969 91.83999938-2.352 23.26399969-4.62400031 46.52800031-7.03999969 70.88000062z m50.064-31.80800062c1.84000031-19.60000031 3.408-36.88000031 5.088-54.15999938 1.45600031-14.91199969 3.19999969-29.79199969 4.43199938-44.72000062 0.336-4.01599969 1.45600031-6.24 5.58400031-5.98399969 14.64 0.97600031 29.72800031 0.03199969 43.80799969 3.42400031 34.20799969 8.27200031 45.67999969 50.91199969 21.888 77.83999969-14.832 16.78399969-34.464 22.11199969-55.55199938 23.52-7.93600031 0.51199969-15.95200031 0.07999969-25.248 0.07999969zM512 439.15200031c-2.04799969-0.60799969-3.312-1.104-4.62400031-1.344-35.35999969-6.44800031-70.96000031-7.80799969-106.54399969-2.64-19.66399969 2.84800031-38.448 8.89600031-53.88799969 21.87199969-21.552 18.11200031-20.28799969 43.92 4.128 58.128 12.24 7.12000031 26.27200031 11.72800031 39.936 16.20799969 13.2 4.32 27.18400031 6.40000031 40.44799969 10.56 11.808 3.67999969 13.13599969 13.42399969 3.36 20.91200062-3.13600031 2.4-6.97600031 4.76800031-10.81600031 5.39199938-10.49599969 1.71199969-21.28000031 4.11199969-31.71199969 3.34400062-22.59199969-1.66399969-45.04000031-5.11999969-68.016-7.90400062-1.27999969 9.95200031-2.75200031 21.648-4.272 33.696 3.91999969 1.27999969 7.2 2.56000031 10.56 3.39200062 31.824 7.968 64.11199969 9.00799969 96.48 4.8 18.83200031-2.448 37.008-7.40800031 53.15200031-17.84000062 26.81599969-17.328 34.39999969-53.616-4.38400031-70.67199938-13.60000031-5.98399969-28.30399969-9.76000031-42.64000031-14.14400062-10.704-3.264-21.85600031-5.26399969-32.36799938-8.928-9.168-3.19999969-10.44799969-11.44000031-2.84800031-17.24799938 4.368-3.32800031 10.04800031-6.55999969 15.36-6.912a450.79999969 450.79999969 0 0 1 50.04799969-0.80000062c14.41600031 0.65599969 28.73599969 3.32800031 43.584 5.184L512 439.15200031z m241.07200031 1.92L735.99999969 607.904h50.76800062c4.48000031-42.78400031 9.10399969-85.056 13.18399969-127.392 0.576-5.96800031 2.208-8.28799969 8.14399969-8.448 10.72000031-0.25600031 21.456-1.85599969 32.14400062-1.47199969 22.24000031 0.79999969 39.744 9.12 36 40.56-2.71999969 22.752-5.24800031 45.53599969-7.728 68.31999938-1.02400031 9.43999969-1.71199969 18.92800031-2.56000031 28.512h51.87199969c3.36-33.05599969 7.488-65.68000031 9.84-98.43199969 3.19999969-44.496-16.656-68.736-60.63999938-75.19999969-38.4-5.63200031-76.25599969-0.31999969-113.952 6.68799938z m-453.28000031 126.192c-8.19199969 1.21600031-15.36 2.62399969-22.59199969 3.264-24.048 2.09599969-48.11200031 3.696-71.53600031-4.00000031-13.03999969-4.30399969-23.136-11.79199969-27.936-24.336-7.40800031-19.40800031 0.83200031-44.16 18.59200031-56.544 16.28800031-11.35999969 35.35999969-14.928 54.79999969-14.928 21.13600031-0.01600031 42.27199969 2.27200031 63.96799969 3.6L320 437.28000031c-14.12800031-1.61599969-27.648-3.67999969-41.24800031-4.608-38.448-2.62399969-75.6 2.448-110.12799938 19.18399969-28.14400031 13.64800031-48.79999969 33.76000031-55.008 63.87199969-7.36000031 35.76 10.81600031 65.15200031 48.528 80.48000062 17.952 7.27999969 36.83200031 11.72800031 56.256 11.808 23.20000031 0.096 46.39999969-1.47199969 69.56799938-2.76800062 2.91199969-0.16000031 7.79200031-2.71999969 8.12800031-4.71999938 1.85599969-10.83199969 2.56000031-21.82399969 3.696-33.24800062z" fill="#888888" p-id="3391"></path></svg>'
        },
        link: 'https://blog.csdn.net/qq_55940788?spm=1000.2115.3001.10640'
      }
    ],

    search: {
      provider: 'local'
    },

    lastUpdated: {
      text: '最后编辑于',
      formatOptions: {
        dateStyle: 'short',
        timeStyle: 'short'
      }
    },
    docFooter: {
      prev: '上一页',
      next: '下一页'
    },
    footer: {
      copyright: 'Copyright © yzk 2025 | <a href="https://beian.miit.gov.cn/" target="_blank">皖ICP备2025092333号-1</a>'
    },
    sidebar: sidebar
  }
})
